// Copyright 2010 Alexandre Tristan St-Cyr <tristan.stcyr@gmail.com>

package com.codeplex.loadjava;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

class JarFileReader {
	
	JarFile jarFile;
	
	public JarFileReader(JarFile jarFile)
	{
		this.jarFile = jarFile;
	}
	
	public void accept(IClassPathVisitor visitor)
	{
		for(Enumeration<JarEntry> entries = jarFile.entries();entries.hasMoreElements();) {
			JarEntry jarEntry = entries.nextElement();
			if (!jarEntry.isDirectory()) {
				String extension = PathHelper.getExtension(jarEntry.getName());
				if (extension.equals("class")) {
					try {
						InputStream inputStream = jarFile.getInputStream(jarEntry);
						visitor.visit(inputStream, jarEntry.getName());
					} catch (IOException e) {
						visitor.onError(e);
					}
				}
			}
		}
	}
}
